package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* CubicToView2
* */
class CubicToView2 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.SQUARE
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_1DE9B6)
        }
    }


    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx + cx / 10, cy)

            val mStrokeWidth = (cx / 15).coerceAtLeast(minStrokeWidth)
            mPaint.strokeWidth = mStrokeWidth

            mPath.apply {
                reset()
                moveTo(-cx * 7 / 10, -cy * 5 / 10)
                cubicTo(
                    cx * 9 / 10,
                    cy * 5 / 10,
                    cx * 9 / 10,
                    -cy * 5 / 10,
                    -cx * 7 / 10,
                    cy * 5 / 10
                )
            }
            drawPath(mPath, mPaint)

            restore()
        }
    }
}